<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:03:51
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_fenjuan extends discuz_table
{

    public function __construct()
    {
        $this->_table         = 'jamesonread_fenjuan';
        $this->_pk            = 'j_id';
        $this->_pre_cache_key = 'jamesonreadfenjuan_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 根据book_id获取书下的所有分卷
    public function fetch_by_bid($book_id)
    {
        if ($cache = $this->fetch_cache('by' . $book_id)) {
            return $cache;
        }
        $data = DB::fetch_all('SELECT * FROM %t WHERE book_id=%d ORDER BY order_num ASC', array($this->_table, $book_id));
        if ($data) {
            $this->store_cache('by' . $book_id, $data);
        }
        return $data;
    }
    // 根据book_id获取所有分卷和之下的章节
    public function fetch_all_bybid($book_id)
    {
        if ($cache = $this->fetch_cache('all' . $book_id)) {
            return $cache;
        }
        $data = array();
        $data = DB::fetch_all('SELECT j_id,j_title,book_id,order_num FROM %t WHERE book_id=%d ORDER BY order_num ASC', array($this->_table, $book_id), 'j_id');
        if ($data) {
            $allcolum = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('j_id', array_keys($data), 'in') . ' ORDER BY j_id,zhangjie ASC,addtime ASC', array('jamesonread_colums'));
            foreach ($allcolum as $key => $value) {
                if ($data[$value['j_id']]) {
                    $data[$value['j_id']]['zhang'][] = $value;
                }
            }
            if ($data) {
                $this->store_cache('all' . $book_id, $data);
            }
        }
        return $data;
    }
}